package com.rover022.flixelgame.manager
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.BinaryDataLoader;
	import com.greensock.loading.DataLoader;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.MP3Loader;
	import com.greensock.loading.SWFLoader;
	import com.greensock.loading.XMLLoader;
	import com.greensock.loading.core.LoaderCore;

	import flash.display.Bitmap;

	public class AssetManager
	{
		private static var _instance:AssetManager;
		private var loadermax:LoaderMax;

		public static function getInstance():AssetManager
		{
			if (null == _instance)
			{
				_instance=new AssetManager(new SingletonEnforcer());
			}
			return _instance;
		}

		public function AssetManager(src:SingletonEnforcer)
		{
			loadermax=new LoaderMax();
			loadermax.maxConnections=1;
			loadermax.addEventListener(LoaderEvent.COMPLETE, onConmplete);
			//loadermax.autoDispose=true;
		}

		/**
		 * 请求资源的方法  一般处理png xml 等等
		 * @param id
		 * @return
		 *
		 */
		public function requestAsset(id:String):*
		{
			return LoaderMax.getContent(id);
		}

		/**
		 * 	请求一张拷贝到位图
		 * @param id
		 * @return
		 *
		 */
		public function requestAssetImageCopy(id:String):Bitmap
		{
			var image:ImageLoader=LoaderMax.getLoader(id);
			var bitmap:Bitmap=new Bitmap((image.rawContent).bitmapData.clone());
			return bitmap;
		}

		/**
		 * 请求swf中一个class的方法
		 * @param swfId
		 * @param classid
		 * @return
		 */
		public function requestAssetClass(swfId:String, classid:String):*
		{
			var swfloader:SWFLoader=LoaderMax.getLoader(swfId);
			if (swfloader)
			{
				return swfloader.getClass(classid);
			}
			else
			{
				return null;
			}
		}

		/**
		 * 请求swf中一个对应的classid 的一个对象实例的方法
		 * @param swfId
		 * @param classid
		 * @return
		 */
		public function requestAssetInstance(swfId:String, classid:String):*
		{
			var swfloader:SWFLoader=LoaderMax.getLoader(swfId);
			if (swfloader)
			{
				var _class:Class=swfloader.getClass(classid);
				if (_class)
				{
					return new _class;
				}
				else
				{
					return null;
				}
			}
			else
			{
				return null;
			}
		}

		/**
		 * 加载管理器队列方式加载资源,支持jpg,png,swf,xml
		 * 加载完毕后会自己立刻释放掉
		 * @param array
		 *
		 */
		public function loadAssetArray(array:Array, eventHandle:Function):void
		{
			var _loadercore:LoaderCore;
			var _realarray:Array=[];
			var name:String; // = _realarray[i];
			for (var j:int=0; j < array.length; j++)
			{
				name=array[j];
				if (LoaderMax.getLoader(name) == null)
				{
					_realarray.push(name);
				}
			}
			if (_realarray.length == 0)
			{
				eventHandle.call(null);
			}
			else
			{
				for (var i:int=0; i < _realarray.length; i++)
				{
					name=_realarray[i];
					if (name.indexOf(".swf") != -1)
					{
						_loadercore=new SWFLoader(name);
						loadermax.append(_loadercore);
					}
					else if (name.indexOf(".jpg") != -1 || name.indexOf(".png") != -1)
					{
						_loadercore=new ImageLoader(name);
						loadermax.append(_loadercore);
					}
					else if (name.indexOf(".xml") != -1 || name.indexOf(".tmx") != -1)
					{
						_loadercore=new XMLLoader(name);
						loadermax.append(_loadercore);
					}
					else if (name.indexOf(".txt") != -1)
					{
						_loadercore=new DataLoader(name);
						loadermax.append(_loadercore);
					}
					else if (name.indexOf(".zip") != -1)
					{
						_loadercore=new BinaryDataLoader(name);
						loadermax.append(_loadercore);
					}
					else if (name.indexOf(".mp3") != -1)
					{
						_loadercore=new MP3Loader(name);
						loadermax.append(_loadercore);
					}
					if (i == (_realarray.length - 1))
					{
						//trace("object:",_loadercore.vars,JSON.stringify(_loadercore.vars))
						_loadercore.vars.callBack=eventHandle;
						_loadercore.addEventListener(LoaderEvent.COMPLETE, onChildComplete);
					}
				}
				loadermax.load();
			}
		}

		protected function onChildComplete(event:LoaderEvent):void
		{
			trace("onChildComplete");
			var child:LoaderCore=event.currentTarget as LoaderCore;
			if (child.vars.callBack)
			{
				child.vars.callBack.call(null);
				child.vars.callBack=null;
			}
		}

		protected function onConmplete(event:LoaderEvent):void
		{
			// TODO Auto-generated method stub
			//trace("onConmplete")
		}
	}
}

class SingletonEnforcer
{
}
